home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_12 / taylor2 / gpibin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-06  |  2.4 KB  |  111 lines

  1. // gpibin.cpp
  2. #include <iostream.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <mem.h>
  6. #include "gpibio.h"
  7.  
  8. // INPUT CLASS FUNCTIONS
  9. gpibin::gpibin(int d, int b) : gpibio(b)
  10. {
  11.     device = dvr[b]->open_device(d);
  12. }
  13.  
  14. ... [full source on code disk -mb]
  15.  
  16. // gin >> double
  17. gpibin & gpibin::operator >> (double & d)
  18. {
  19.     char c;
  20.  
  21.     // char sure we are pointing at a valid character
  22.     do
  23.       c = sbumpc();
  24.     while(!isdigit(c) && c != '-' && c!= '+' && c!= '.' && c!= 'e' && c!= 'E');
  25.  
  26.     // Convert input string to a double
  27.     // via strtod which advances the char pointer
  28.     d = strtod(gptr_-1,&gptr_);
  29.     return *this;
  30. }
  31.  
  32. ...
  33.  
  34. // gin >> manipulator support function
  35. gpibin & gpibin::operator>> (gpibin & (*_f)(gpibin &))
  36. {
  37.     return _f(*this);
  38. }
  39.  
  40. // do the wait for SRQ
  41. int gpibin::srqwait()
  42. {
  43.    char status;
  44.    int result;
  45.  
  46.    ::ibwait(device,TIMO|RQS);
  47.    if(ibsta &(TIMO|ERR))
  48.       return (-1);   // Timed out or device error
  49.  
  50.    // Get Serial poll information
  51.    ::ibrsp(device,&status);
  52.    result = status;
  53.  
  54.    return (result&255);     // Return the last status
  55. }
  56.  
  57. // Dummy overflow
  58. void gpibin::overflow()
  59. {
  60. }
  61.  
  62. // Fill input stream buffer
  63. int gpibin::underflow()
  64. {
  65.     int count,growth,inplimit;
  66.     char *temp;
  67.  
  68.     // See if we have allocated a stream buffer yet
  69.     if(base_ == NULL)
  70.     {
  71.        gleng_ = 128;      // a good starting length
  72.        base_ = new char[gleng_];
  73.        if(base_ == NULL)
  74.        {
  75.            cerr << "Can't allocate get stream buffer." << endl;
  76.            exit(1);
  77.        }
  78.        ebuf_ = base_ + gleng_;
  79.     }
  80.     inplimit = gleng_-1;
  81.     // Get entire gpib transfer
  82.     for(count=0;;)
  83.     {
  84.         ::ibrd(device,base_+count,inplimit);
  85.         status = ibsta;      // ibsta & ibcnt are NI globals
  86.         count += ibcnt;
  87.         if(status & (ERR|TIMO|END))  // Done if EOI or error
  88.            break;
  89.         growth = gleng_ >> 1;
  90.         temp = new char[gleng_ + growth];
  91.         if(temp == NULL)
  92.         {
  93.             cerr << "Can't increase get stream size." << endl;
  94.             exit(1);
  95.         }
  96.         // transfer to new area & release old area
  97.         memmove(temp,base_,gleng_);
  98.         delete base_;
  99.         base_ = temp;
  100.         inplimit = growth-1;
  101.         gleng_ += growth;
  102.     }
  103.     base_[count] = '\0';
  104.     if(base_[count-1] == '\n')
  105.       base_[--count] = '\0';
  106.     gptr_ = base_;
  107.     egptr_ = base_ + count;           // Set pointer to end of get portion
  108.     return *gptr_;
  109. }
  110.  
  111.